##########
#Countering Authoritarian Behavior in Democracies
#Sara B. Hobolt, Moritz Osnabruegge
#This script produces Figures 2-3, and A2-A5 
#########


library(cregg)
library(foreign)
library(gdata)
library(ggplot2)

data <- read.dta("data_analysis.dta")

source("themes.R")
source("functions.R")

a <- as.data.frame(matrix(c("Controversy:",1,1,1,1), nrow=1, ncol=5))
colnames(a) <- c("var","pe","se","ul","ll")
b <- as.data.frame(matrix(c("Actor Reacting:",1,1,1,1), nrow=1, ncol=5))
colnames(b) <- c("var","pe","se","ul","ll")
c <- as.data.frame(matrix(c("Reaction:", 1,1,1,1), nrow=1, ncol=5))
colnames(c) <- c("var","pe","se","ul","ll")
d <- as.data.frame(matrix(c("Gender:", 1,1,1,1), nrow=1, ncol=5))
colnames(d) <- c("var","pe","se","ul","ll")
e <- as.data.frame(matrix(c("Party:", 1,1,1,1), nrow=1, ncol=5))
colnames(e) <- c("var","pe","se","ul","ll")
f <- as.data.frame(matrix(c("Experience:", 1,1,1,1), nrow=1, ncol=5))
colnames(f) <- c("var","pe","se","ul","ll")



######
##Figure 2
######

f1 <- chosen ~ Controversy + ReactionxActor + ReactionxAction + Gender + Party + Minister 

out1 <- mm(data, f1, id =~ID)

out1 <- prep_data2(out1)

out1 <- out1[10:18,]

out1$var <- factor(out1$var,levels=unique(out1$var)[length(out1$var):1])

p1 <- ggplot(data=out1) + coord_flip(ylim=c(.425,.575)) +
      geom_hline(yintercept=0.5,linewidth=.5,colour="darkgrey",linetype="dotted") +
      geom_pointrange(mapping=aes(y=pe,x=var,ymin=ll,ymax=ul),size=.5,position=position_dodge(width=.75),na.rm=T) +
      scale_y_continuous(name="Marginal Means",breaks=c(.45,.5,.55),labels=c("0.45","0.5","0.55")) +
      scale_x_discrete(name="") +
      theme_bw1()


pdf("fig_2.pdf",width=9,height=3)
print(p1)
dev.off()



#####
##Figures 3 and A2
#####

out2 <- mm_diffs(data, f1, ~ party_identity, id=~ ID)

out2 <- prep_data2(out2)

out21 <- out2[10:18,]

out21$var <- factor(out21$var,levels=unique(out21$var)[length(out21$var):1])

p21 <- ggplot(data=out21) + coord_flip(ylim=c(-.07,.07)) +
       geom_hline(yintercept=0,linewidth=.5,colour="darkgrey",linetype="dotted") +
       geom_pointrange(mapping=aes(y=pe,x=var,ymin=ll,ymax=ul),size=.5, position=position_dodge(width=.75), na.rm=T) +
       scale_y_continuous(name="Difference in Marginal Means (Labour - Conservative)",breaks=c(-.05,-.025,0,.025,.05), labels=c("-.05","-.025","0",".025",".05")) +
       scale_x_discrete(name="") +
       theme_bw1()

pdf("fig_3.pdf",width=9,height=4)
print(p21)
dev.off()

out2$var <- factor(out2$var,levels=unique(out2$var)[length(out2$var):1])

p2 <- ggplot(data=out2) + coord_flip(ylim=c(-.2,.2)) +
      geom_hline(yintercept=0,linewidth=.5,colour="darkgrey",linetype="dotted") +
      geom_pointrange(mapping=aes(y=pe,x=var,ymin=ll,ymax=ul),size=.5, position=position_dodge(width=.75), na.rm=T) +
      scale_y_continuous(name="Difference in Marginal Means (Labour - Conservative)",breaks=c(-.2,-.1,0,.1,.2),labels=c("-.2","-.1","0",".1",".2"))+ 
      scale_x_discrete(name="") +
      theme_bw1()


pdf("fig_A2.pdf",width=9,height=5)
print(p2)
dev.off()



#####
##Figure A3
#####

out3 <- mm_diffs(data, f1, ~ pluralism, id = ~ ID)
out3 <- prep_data2(out3)

out3$var <- factor(out3$var,levels=unique(out3$var)[length(out3$var):1])

p3 <- ggplot(data=out3) + coord_flip(ylim=c(-.2,.2)) +
      geom_hline(yintercept=0,linewidth=.5,colour="darkgrey",linetype="dotted") +
      geom_pointrange(mapping=aes(y=pe,x=var,ymin=ll,ymax=ul),size=.5, position=position_dodge(width=.75), na.rm=T) +
      scale_y_continuous(name="Difference in Marginal Means (High - Low Pluralism)",breaks=c(-.2,-.1,0,.1,.2),labels=c("-.2","-.1","0",".1",".2")) +
      scale_x_discrete(name="") +
      theme_bw1()

pdf("fig_A3.pdf",width=9,height=5)
print(p3)
dev.off()



#####
##Figure A4
#####

out4 <- mm_diffs(data, f1, ~ authoritarianism, id = ~ ID)

out4 <- prep_data2(out4)

out4$var <- factor(out4$var,levels=unique(out4$var)[length(out4$var):1])

p4 <- ggplot(data=out4) + coord_flip(ylim=c(-.2,.2)) +
      geom_hline(yintercept=0,linewidth=.5,colour="darkgrey",linetype="dotted") +
      geom_pointrange(mapping=aes(y=pe,x=var,ymin=ll,ymax=ul),size=.5, position=position_dodge(width=.75), na.rm=T) +
      scale_y_continuous(name="Difference in Marginal Means (High - Low Authoritarianism)",breaks=c(-.2,-.1,0,.1,.2),labels=c("-.2","-.1","0",".1",".2")) +
      scale_x_discrete(name="") +
      theme_bw1()

pdf("fig_A4.pdf",width=9,height=5)
print(p4)
dev.off()


#####
##Figure A5
#####

out5 <- mm_diffs(data, f1, ~ democracy, id = ~ ID)

out5 <- prep_data2(out5)

out5$var <- factor(out5$var,levels=unique(out5$var)[length(out5$var):1])

p5 <- ggplot(data=out5) + coord_flip(ylim=c(-.2,.2)) +
      geom_hline(yintercept=0,linewidth=.5,colour="darkgrey",linetype="dotted") +
      geom_pointrange(mapping=aes(y=pe,x=var,ymin=ll,ymax=ul),size=.5, position=position_dodge(width=.75), na.rm=T) +
      scale_y_continuous(name="Difference in Marginal Means (High - Low Democracy)",breaks=c(-.2,-.1,0,.1,.2),labels=c("-.2","-.1","0",".1",".2")) +
      scale_x_discrete(name="") +
      theme_bw1()

pdf("fig_A5.pdf",width=9,height=5)
print(p5)
dev.off()